$(document).ready(function () {
  // var vConsole = new VConsole();
  new Swiper('.swiper-container', {
    spaceBetween: 0,
    centeredSlides: true,
    effect: 'slide',
    speed: 800,
    autoplay: {
      delay: 2500,
      disableOnInteraction: false,
    },
    pagination: {
      el: '.swiper-pagination',
      clickable: true,
    },
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
  });

  var host = "http://106.14.140.51:8080";
  // var host = "http://127.0.0.1:8080";
  
  function toast(msg) {
    $("#toast")
      .html(msg)
      .fadeIn()
      .delay(3000)
      .fadeOut();
  }

  $('.ws-li').on('click', function() {
    $('.ws-li').removeClass('ws-active');
    $(this).addClass('ws-active');
    var idx = $(this).parent().index() + 1;
    $(".box").animate({ left: "100px", opacity: "0" }, 100).hide();
    $(".box" + idx).show().animate({ left: "0", opacity: "1" }, 500);
  })

  $("#center").click(function(e) {
    e.stopPropagation();
    $(this).toggleClass('open')
  })

  $(".center-list li").click(function () {
    $('input[name=center]').val($(this).html())
  })

  $(document).click(function(e) {
    $("#center").removeClass("open");
  })

  jQuery.validator.addMethod("tel", function(value) {
    var telmatch = /^1[0-9]{10}$/;
    return telmatch.test(value);
  }, "手机号码不正确");

  jQuery.validator.addMethod("email", function(value) {
    var telmatch = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    return telmatch.test(value);
  }, "邮箱地址不正确");

  $("#form").validate({
    errorElement: "span",
    errorPlacement: function(error, element) {
      element.parents(".input-line").append(error);
    },
    success: function(span) {
      span.remove();
    },
    submitHandler: function (form) {
      $.ajax({
        url: host + "/api/submit",
        method: "post",
        data: {
          username: $("input[name=username]").val(),
          email: $("input[name=email]").val(),
          tel: $("input[name=tel]").val(),
          center: $("input[name=center]").val()
        },
        success: function(res) {
          if (res.code == 1) {
            $(".mask-wrap").show();
            $("#form")[0].reset();
          } else {
            toast("提交失败");
          }
        },
        error: function() {
          toast("提交失败");
        }
      });
    },
    rules: {
      username: {
        required: true
      },
      email: {
        required: true,
        email: "email"
      },
      tel: {
        required: true,
        tel: "tel"
      },
      center: {
        required: true,
      }
    },
    messages: {
      username: {
        required: "请输入姓名"
      },
      email: {
        required: "请输入邮箱"
      },
      tel: {
        required: "请输入手机号"
      },
      center: {
        required: "请选择预约中心"
      }
    }
  });

  $(".order-btn").click(function() {
    $("#form").submit()
  })
  $(document).on("scroll", function() {
    var scrollTop = $(window).scrollTop();
    if (scrollTop > 700) {
      $('.fix-btn').show();
    } else {
      $(".fix-btn").hide();
    }
  });

  $('.close').click(function() {
    $(this).parents('.mask-wrap').hide();
  })

  // $.ajax({
  //   url: host + "/api/sign",
  //   method: "post",
  //   dataType: "json",
  //   data: {
  //     url: window.location.href.split("#")[0]
  //   },
  //   error: function() {
  //     toast("获取签名失败");
  //   },
  //   success: function(res) {
  //     if (res.code !== 1) {
  //       toast("获取签名失败");
  //       return;
  //     }
  //     wx.config({
  //       debug: true,
  //       appId: res.data.appid,
  //       timestamp: res.data.timestamp,
  //       nonceStr: res.data.noncestr,
  //       signature: res.data.signature,
  //       jsApiList: [
  //         "onMenuShareTimeline",
  //         "onMenuShareAppMessage",
  //         "onMenuShareQQ"
  //       ]
  //     });
  //     wx.ready(function() {
  //       console.log("ready");
  //       wx.onMenuShareTimeline({
  //         title: "分享标题", // 分享标题
  //         desc: "分享描述", // 分享描述
  //         link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  //         imgUrl: "" // 分享图标
  //       });
  //       wx.onMenuShareAppMessage({
  //         title: "", // 分享标题
  //         link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  //         imgUrl: "" // 分享图标
  //       });
  //     });
  //   }
  // });

});